<?php


namespace app\service\config;


use app\model\partner\PartnerBusyTimeModel;

class DutyService
{
    /**
     * 设置忙碌
     * @param $partnerId
     * @param $reserveTime
     * @param $duration
     * @param $orderId
     * @throws \Exception
     */
    public static function setBusyTime($partnerId, $reserveTime, $duration, $orderId = 0)
    {
        // 教练排班更新
        if ($orderId) {
            PartnerBusyTimeModel::where('order_id', $orderId)->where('partner_id', $partnerId)->delete();
        }
        $startTime = date('H', $reserveTime);
        $day = strtotime(date("Y-m-d", $reserveTime));
        $dataAll = [];
        $j = 0;
        for ($i = 0; $i <= $duration; $i++) {
            $time = $startTime + $i;
            $dataAll[$j] = ['time' => $time, 'partner_id' => $partnerId, 'day' => $day, 'order_id' => $orderId];
            $j++;
        }
        $partnerBusyTimeModel = new PartnerBusyTimeModel();
        $partnerBusyTimeModel->saveAll($dataAll);
    }

    /**
     * 取消忙碌
     * @param $partnerId
     * @param $reserveTime
     * @param $duration
     * @param $orderId
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function cancelBusyTime($partnerId, $reserveTime, $duration, $orderId = 0)
    {
        // 教练排班更新
        $startTime = date('H', $reserveTime);
        $day = strtotime(date("Y-m-d", $reserveTime));
        for ($i = 0; $i <= $duration; $i++) {
            $time = $startTime + $i;
            $info = PartnerBusyTimeModel::where('partner_id', $partnerId)->where('day', $day)->where('time', $time)->find();
            if ($info) {
                $info->delete();
            }
        }
        return true;
    }
}